﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using Models;
using System.Data.Linq;
using System.Linq.Expressions;
namespace BAL
{
    public class TestPaperPolicySub : BaseBAL<TestPaperPolicySubInfo, Models.TestPaperPolicySub>
    {
        public static IList<TestPaperPolicySubInfo> GetList(int mainPolicyId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<Models.TestPaperPolicySub>().Where(p => p.PolicyMainId.HasValue && p.PolicyMainId.Value == mainPolicyId).OrderBy(t => t.Order).Select(t => TransT2ToT1(t)).ToList();
            }
        }
        public static void DeleteByMainPolicyId(int mainPolicyId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<int> ids= db.GetTable<Models.TestPaperPolicySub>().Where(p =>p.PolicyMainId.HasValue && p.PolicyMainId.Value == mainPolicyId ).Select(p=>p.Id).ToList();
                Delete(ids);
            }
        }
        public static IList<TestPaperPolicySubInfo>  GetListByExamPaperId(int examPaperId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                var query = from subPolicyItem in db.GetTable<Models.TestPaperPolicySub>()
                            from mainPolicyItem in db.GetTable<Models.TestPaperPolicyMain>()
                            from examPaperItem in db.GetTable<Models.ExamPaper>()
                            where examPaperItem.Id == examPaperId
                                  && examPaperItem.TestPaperPolicyMainId.HasValue
                                  && examPaperItem.TestPaperPolicyMainId.Value == mainPolicyItem.Id
                                  && subPolicyItem.PolicyMainId.HasValue
                                  && subPolicyItem.PolicyMainId.Value == mainPolicyItem.Id
                            select subPolicyItem;

                return query.Select(t => TransT2ToT1(t)).ToList();
              
            }
        }
    }
}
